﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kinect.Framework.Interfaces;
using ManagedNite;
using System.Windows;
using Kinect.Framework.Lib;
using System.Windows.Controls;

namespace Kinect.Framework.Controls
{
    public class KinectContextMenuItem : KinectButton
    {
        #region GlobalOffsetX
        /// <summary>
        /// The GlobalOffsetX Dependency
        /// </summary>
        public static readonly DependencyProperty GlobalOffsetXProperty = DependencyProperty.RegisterAttached("GlobalOffsetX", typeof(double), typeof(KinectContextMenuItem), new FrameworkPropertyMetadata((double)0.0, new PropertyChangedCallback(OnGlobalOffsetXChanged)));
        /// <summary>
        /// The Header of your item
        /// </summary>
        public double GlobalOffsetX
        {
            get { return (double)GetValue(KinectContextMenuItem.GlobalOffsetXProperty); }
            set
            {
                SetValue(KinectContextMenuItem.GlobalOffsetXProperty, value);
            }
        }
        /// <summary>
        /// Handles changes to the GlobalOffsetX property.
        /// </summary>
        private static void OnGlobalOffsetXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as KinectContextMenuItem;
            Canvas.SetLeft(control, control.GlobalOffsetX);
        }
        #endregion

        #region GlobalOffsetY
        /// <summary>
        /// The GlobalOffsetY Dependency
        /// </summary>
        public static readonly DependencyProperty GlobalOffsetYProperty = DependencyProperty.RegisterAttached("GlobalOffsetY", typeof(double), typeof(KinectContextMenuItem), new FrameworkPropertyMetadata((double)0.0, new PropertyChangedCallback(OnGlobalOffsetYChanged)));
        /// <summary>
        /// The Header of your item
        /// </summary>
        public double GlobalOffsetY
        {
            get { return (double)GetValue(KinectContextMenuItem.GlobalOffsetYProperty); }
            set
            {
                SetValue(KinectContextMenuItem.GlobalOffsetYProperty, value);
            }
        }
        /// <summary>
        /// Handles changes to the GlobalOffsetY property.
        /// </summary>
        private static void OnGlobalOffsetYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as KinectContextMenuItem;
            Canvas.SetTop(control, control.GlobalOffsetY);
        }
        #endregion

        public bool IsItemFocused { get; private set; }

        public KinectContextMenuItem()
        {
            KinectManipulation.SetIsSelectable(this, true);
            KinectManipulation.AddClickHandler(this, OnSelected);
        }
        private void OnSelected(object sender, RoutedEventArgs e)
        {
            var contextMenu = VisualTreeHelpers.FindVisualParent<KinectContextMenu>(this);
            contextMenu.SelectedItem = this;
            contextMenu.OnOnKinectItemSelected(this);
        }
        public override void GotKinectFocus(XnMPoint point, XnMPoint realPoint, UIElement OriginalSource)
        {
            base.GotKinectFocus(point, realPoint, OriginalSource);
            IsItemFocused = true;
            var contextMenu = VisualTreeHelpers.FindVisualParent<KinectContextMenu>(this);
            contextMenu.GotKinectFocus(point, realPoint, OriginalSource);

        }
        public override void LostKinectFocus()
        {            
            base.LostKinectFocus();
            IsItemFocused = false; 
            var contextMenu = VisualTreeHelpers.FindVisualParent<KinectContextMenu>(this);
            contextMenu.LostKinectFocus();
        }
    }
}
